package com.kidback.bbloc259.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.kidback.bbloc259.databinding.CustomTitleBarBinding;


/**
 * Created by meikai on 2020/06/02.
 */
public class CustomTitleBarView extends FrameLayout {

    private CustomTitleBarBinding binding;
    private OnClickListener onBackClickListener;

    public CustomTitleBarView(@NonNull Context context) {
        this(context, null);
    }

    public CustomTitleBarView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomTitleBarView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        binding = CustomTitleBarBinding.inflate(LayoutInflater.from(getContext()), this, true);

        binding.imgBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onBackClickListener != null) {
                    onBackClickListener.onClick(v);
                }
            }
        });
    }

    public void setGravityTop(){
        binding.tvTitle.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL);
    }

    public void setTintColor(int color) {
        binding.imgBackReal.setImageTintList(ColorStateList.valueOf(color));
        binding.imgBackReal.setBackgroundTintList(ColorStateList.valueOf(color));
        binding.tvTitle.setTextColor(color);
    }

    public void setOnBackClickListener(OnClickListener onBackClickListener) {
        this.onBackClickListener = onBackClickListener;
    }

    public void setTitle(String title) {
        binding.tvTitle.setText(title);
    }

    public void setTitleTextSize(int sizeSp) {
        binding.tvTitle.setTextSize(sizeSp);
    }

    public void hideBackIcon() {
        binding.imgBack.setVisibility(View.INVISIBLE);
    }

    public void showDividerLine() {
        binding.viewBottom.setVisibility(View.VISIBLE);
    }

    public void hideDividerLine() {
        binding.viewBottom.setVisibility(View.GONE);
    }

}
